<template>
  <div>
    <el-tabs v-model="tab" @tab-click="tabClick">
      <el-tab-pane label="企业海报夹" name="0"></el-tab-pane>
      <el-tab-pane label="我创建的海报" name="1" v-if="showTabs"></el-tab-pane>
    </el-tabs>
    <div class="card-list">
      <div class="card-item" v-for="item in tableList" :key="item.id">
        <span class="check">
          <el-checkbox v-model="item.checked" @change="checkChange($event,item)"></el-checkbox>
          <div>
            <el-tag size="mini" @click="showImage(item)">预览</el-tag>
          </div>
        </span>
        <el-image style="width: 140px; height: 160px" fit="contain" :src="item.coverUrl"></el-image>
        <div class="text-info">
          <p class="title">{{ item.materialName }}</p>
          <p class="verify">{{ item.examineBy }}</p>
          <p class="time">{{ item.createTime }}</p>
        </div>
      </div>
    </div>
    <div class="page">
      <NormalPagination :currentPage="pageNum" :pageSize="pageSize" :total="total" layout="prev, pager, next" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
    <div class="btn-list">
      <el-button type="info" plain @click="selectCancelFn">取消</el-button>
      <el-button type="primary" @click="selectSureFn">确定</el-button>
    </div>
  </div>
</template>

<script>
import { posterList } from '@/api/article/group'

export default {
  props: {
    linkList: {
      type: Array,
      default: () => []
    },
    showTabs: {
      type: Boolean,
      default: true,
    }
  },
  data() {
    return {
      tab: '0',
      pageNum: 1,
      pageSize: 5,
      total: 0,
      tableList: [],
      currentSelectList: [],
    }
  },
  mounted() {
    this.currentSelectList = JSON.parse(JSON.stringify(this.linkList))
    this.getList(this.tab)
  },
  methods: {
    checkChange(e, item) {
      if (e) {
        this.currentSelectList.push({
          mediaType: '5',
          name: item.materialName,
          coverUrl: item.coverUrl,
          id: item.id,
          materialType: '5',
          materialName: item.materialName,
          materialUrl: item.coverUrl,
        })
      } else {
        this.currentSelectList.forEach((element, index) =>{
          if (item.mediaType == 5 && element.id == item.id)
            this.currentSelectList.splice(index, 1);
        })
      }
    },
    tabClick() {
      this.pageNum = 1
      this.tableList = [];
      this.total = 0;
      this.getList(this.tab)
    },
    getList(levelType) {
      posterList({
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        mediaType: 5,
        categoryId: '',
        search: '',
        levelType,
      }).then(res => {
        if (res.code === 200) {
          const { rows, total } = res.data
          this.tableList = rows.map(item => {
            return {
              ...item,
              checked: false,
              coverUrl: window.globalConfig.IMAGE_API + item.coverUrl
            }
          })
          this.currentSelectList.length > 0 && this.tableList.forEach(element => {
            this.currentSelectList.forEach(item => {
              if (item.mediaType == 5 && element.id == item.id) element.checked = true;
            })
          })
          this.total = total
        }
      })
    },
    handleCurrentChange(val){
      this.pageNum = val
      this.getList(this.tab)
    },
    selectCancelFn() {
      this.$emit("selectCancelFn");
    },
    selectSureFn(){
      this.$emit("confirm", this.currentSelectList);
    },
    showImage(item){
      this.$emit('view', item)
    }
  }
}
</script>


<style lang="scss" scoped>
.card-list {
  display: flex;
  flex-wrap: wrap;
  min-height: 264px;
  .card-item {
    position: relative;
    width: 152px;
    margin-right: 20px;
    margin-bottom: 20px;
    .check{
      position: absolute;
      top: 10px;
      right: 10px;
      z-index: 99;
      text-align: center;
      .el-tag{
        cursor: pointer;
      }
    }
    &:nth-child(4n){
      margin-right: 0;
    }
  }

}
.page{
  text-align: right;
}
.btn-list{
  margin-top: 10px;
  text-align: right;
}

.text-info {
  display: flex;
  flex-direction: column;
  line-height: 2;

  .title {
    color: #606266;
  }

  .verify {
    text-align: right;
    color: #082aff;
  }

  .time {
    text-align: right;
    color: #999;
  }
}
</style>
